package com.example.imageloaderdemo.imageloader;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import java.io.FileDescriptor;

/**
 * 图片压缩类
 *
 * @author wdl
 */
public class ImageResizer {
    private static final String TAG = "ImageResizer";

    public ImageResizer() {

    }

    /**
     * 从Resource采样Bitmap
     *
     * @param resources 资源
     * @param resId     资源id
     * @param reqWidth  要求的宽
     * @param reqHeight 要求的高
     * @return 采样后的Bitmap
     */
    public Bitmap decodeSampledBitmapFromResource(Resources resources, int resId, int reqWidth, int reqHeight) {
        //首先将inJustDecodeBounds设置为true,仅解析图片原始宽高不加载图片内容
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(resources, resId, options);
        //计算采用率
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        //按照采用率设置bitmap
        //inJustDecodeBounds=false加载bitmap
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(resources, resId, options);
    }

    /**
     * 从文件描述符采样Bitmap
     *
     * @param fileDescriptor 文件描述符
     * @param reqWidth       要求的宽
     * @param reqHeight      要求的高
     * @return 采样后的Bitmap
     */
    public Bitmap decodeSampledBitmapFromFileDescriptor(FileDescriptor fileDescriptor, int reqWidth, int reqHeight) {
        //首先将inJustDecodeBounds设置为true,仅解析图片原始宽高不加载图片内容
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;

        BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);

        //计算采用率
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        //按照采用率设置bitmap
        //inJustDecodeBounds=false加载bitmap
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
    }

    /**
     * 计算采用率
     *
     * @param options   Bitmap的Options记录获取图片的原始大小
     * @param reqWidth  要求的宽
     * @param reqHeight 要求的高
     * @return 采用率
     */
    private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        //如果其中一个为0则采用率1不缩放了
        if (reqWidth == 0 || reqHeight == 0) {
            return 1;
        }

        //获取图片的原始大小
        final int height = options.outHeight;
        final int width = options.outWidth;

        Log.d(TAG, "calculateInSampleSize: width=" + width + ",height=" + height);

        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            //计算采用率的值最大，同时满足图片宽高>要求的宽高，这样取到的图片较小且合适显示大小
            while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) {
                inSampleSize *= 2;
            }
        }
        Log.d(TAG, "calculateInSampleSize: " + inSampleSize);
        return inSampleSize;
    }
}
